﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Warehouse.DataAccess
{
    public class DataAccessManager
    {
        #region Singleton

        private static DataAccessManager _instance = null;
        private static object lockObject = new object();

        public static DataAccessManager Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (lockObject)
                    {
                        if (_instance == null)
                        {
                            _instance = new DataAccessManager();
                        }
                    }
                }
                return _instance;
            }
        }

        #endregion

        private readonly DataAccessProvider _provider;

        //--Constructor
        private DataAccessManager()
        {
            var asr = new System.Configuration.AppSettingsReader();
            string daProviderFileName = asr.GetValue("DataAccessProviderFileName", typeof(string)).ToString();
            string daProviderClassName = asr.GetValue("DataAccessProviderClassName", typeof(string)).ToString();

            string s = System.IO.Directory.GetCurrentDirectory() + "\\" + daProviderFileName;
            var assembly = System.Reflection.Assembly.LoadFile(s);

            _provider = (DataAccessProvider)assembly.CreateInstance(daProviderClassName);
        }

        public DataAccessProvider Provider
        {
            get { return _provider; }
        }
    }
}
